package com.example.asus.zyfypt_tea_307plx.activities;


import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;


import com.example.asus.zyfypt_tea_307plx.R;
import com.example.asus.zyfypt_tea_307plx.service.TwareDetailService;
import com.github.barteksc.pdfviewer.PDFView;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TwareDetailActivity extends AppCompatActivity {
    private PDFView pdfView;

    private MenuItem menuItem;
    private String path;
    //设置Retrofit访问网络的超时时间
    private static final OkHttpClient client =
            new OkHttpClient.Builder().
                    connectTimeout(600, TimeUnit.SECONDS).
                    readTimeout(600, TimeUnit.SECONDS).
                    writeTimeout(600, TimeUnit.SECONDS).build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("TwareDetailActivity--------->37");
        setContentView(R.layout.activity_tware_detail);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        path = getIntent().getStringExtra("pdfattach");
        pdfView = (PDFView) findViewById(R.id.pdfView);
        //显示进度
        setLoadingState(true);
        //加载PDF
        loadPdf();
    }

    private void loadPdf() {

        String baseUrl = "http://amicool.neusoft.edu.cn/Uploads/";
        Retrofit retrofit =
                new Retrofit.Builder()
                        .baseUrl(baseUrl)
                        .client(client)
                        .build();
        TwareDetailService service = retrofit.create(TwareDetailService.class);

        final Call<ResponseBody> call = service.getPdf(path);
//        http://amicool.neusoft.edu.cn/Uploads/tware/pdfattach/20170518/20170518011952_1043548712.pdf
        System.out.println("----------------> Pdfpath  " + path);
        System.out.println("----------------> Pdfpath   " + path);


        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                if (response.isSuccessful()) {

                    try {
                        byte[] data = response.body().bytes();

                        //加载PDF
                        pdfView.fromBytes(data).swipeHorizontal(true).load();
                        //隐藏进度
                        setLoadingState(false);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }


    public void setLoadingState(boolean refreshing) {

        if (refreshing) {
            // menuItem.setActionView(R.layout.progress);
        } else {
            // menuItem.setActionView(null);
        }
    }
}






















//public class TwareDetailActivity extends AppCompatActivity {
//    private PDFView pdfView;
//
//    private MenuItem menuItem;
//    private String path;
//    //设置Retrofit访问网络的超时时间
////    private static final OkHttpClient client =
////            new OkHttpClient.Builder().
////                    connectTimeout(600, TimeUnit.SECONDS).
////                    readTimeout(600, TimeUnit.SECONDS).
////                    writeTimeout(600, TimeUnit.SECONDS).build();
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        setContentView(R.layout.activity_tware_detail);
//        path = getIntent().getStringExtra("pdfattach");
//
//        pdfView = (PDFView) findViewById(R.id.pdfView);
//        //显示进度
//        //加载PDF
//        loadPdf();
//    }
//
//    private void loadPdf() {
//        String baseUrl = "http://amicool.neusoft.edu.cn/Uploads/";
//        String temp = "http://amicool.neusoft.edu.cn/Uploads/"+path;
//        Uri uri = Uri.parse(temp);
//        pdfView.fromUri(uri);
//        String pdfName = Environment.getExternalStorageState()+"/download";
//        File file = new File(pdfName,"testPic.pdf");
//        pdfView.fromUri(uri)
//                .defaultPage(1)
//                .enableDoubletap(true)
//                .onDraw(this)
//                .onPageChange(this)
//                .enableAnnotationRendering(true)
//                .onLoad(this)
//                .enableDoubletap(true)
//                .swipeVertical(true)
//                .load();
//        }
//}
